function getCurrentDate(){
	var localDate = new Date();
	var currentDate=localDate.toLocaleDateString();
	$("#currentDate").html(currentDate);
}
//获取双周列表
function getDoubleWeekWorkList(){
	var path ="/"+getRootPath()+"/pmc/getDoubleWeekList.do";
	$.ajax({
		cache:false,//是否缓存
		type:"POST",//提交方式
		url:path,
		async:false,//是否异步
		dataType:"json",
		error:function(request){//错误时
			alert("链接错误！");
		},
		success:function(data){
			var dwContent = "<tr><td colspan='4'>双周前期数据</td></tr>";
			var tdContent = "";
			for(var i=0;i<data.length;i++){
				var obj = data[i];
				var currentSeq = $("input[name='seq']").val();
				var href = "";
				if((currentSeq*1)>=(obj.seq*1)){
					href = "<a class='sz' href='javascript:void(0)' onclick='locadHistory(\""+obj.seq+"\",this)'>双周"+obj.seq+" ["+obj.startdate+"~"+obj.dateline+"]</a>";
				} else {
					href = "<span style='color:#ccc;'>双周"+obj.seq+" ["+obj.startdate+"~"+obj.dateline+"]</span>";
				}
				tdContent += (""
					+"<td>"+href+"</td>"
				);
				if((i+1)%4==0|| (i+1)==data.length){
					dwContent += ("<tr>"+tdContent+"</tr>");
					tdContent = "";
				}
			}
			$("#dw").html(dwContent);
		}
	});
}

//获取双周当前
function getCurrentDoubleWeek(){
	var path = "/"+getRootPath()+"/pmc/getCurrentDoubleweek.do";
	$.ajax({
		cache:false,//是否缓存
		type:"POST",//提交方式
		url:path,
		async:false,//是否异步
		dataType:"json",
		error:function(request){//错误时
			alert("链接错误！");
		},
		success:function(data){
			$("#startdate").html(data.startdate);
			$("#dateline").html(data.dateline);
			$("input[name='seq']").val(data.seq);
		}
	});
}
//获取月列表
function getMonthList(){
	var path ="/"+getRootPath()+"/pmc/workProduct/getMonthList.do";
	$.ajax({
		cache:false,
		type:"POST",
		url:path,
		async:false,
		dataType:"json",
		error:function(request){
		alert("错误链接！");
	},
		success:function(data){
		var dwContent = "<tr><td colspan='4'>月前期数据</td></tr>";
		var tdContent = "";
		for(var i=0;i<data.length;i++){
			var obj = data[i];
			var currentSeq = $("input[name='monthSeq']").val();
			var href = "";
			if((currentSeq*1)>=(obj.seq*1)){
				href = "<a class='sz' href='javascript:void(0)' onclick='locadHistory(\""+obj.seq+"\",this)'>月"+obj.seq+" ["+obj.yearsmonths+"]</a>";
			} else {
				href = "<span style='color:#ccc;'>月"+obj.seq+" ["+obj.yearsmonths+"]</span>";
			}
			tdContent += (""
				+"<td>"+href+"</td>"
			);
			if((i+1)%4==0|| (i+1)==data.length){
				dwContent += ("<tr>"+tdContent+"</tr>");
				tdContent = "";
			}
		}
		$("#dw").html(dwContent);
	}
	});
}
//获取 当前月
function getCurrentMonth(){
	var path = "/"+getRootPath()+"/pmc/workProduct/getCurrentMonth.do";
	$.ajax({
		cache:false,
		type:"POST",
		url:path,
		async:false,
		dataType:"json",
		error:function(request){
		alert("链接错误！");
	},
		success:function(data){
		$("#currentMonthDate").html(data.yearsmonths);
		$("input[name='monthSeq']").val(data.seq);
	}
	});
}
//提交页面数据
function submitGj() {
		var collectDate = $("#currentDate").text();
		var path = "/"+getRootPath()+"/pmc/saveWorkProductSnapshoot.do";
		$.ajax({
			cache:false,
			type:"POST",
			url:path,
			async:false,
			dataType:"json",
			data : {
				"collectDate" : collectDate,
				"parameter" : JSON.stringify(GetSubmitJsonData())
			},
			error:function(request){
				window.location.reload();
			},
			success:function(data){
				
			}
	});
}
//获取页面数据
function GetSubmitJsonData() {
	var jsonArray = new Array();
	$("#tb").children("tr").each(function(i) {
		//拼接准备上传的 json 串
		var jsonObj = {
				//快照节点（第几个双周啊，第几个月之类的，根据snapshoottype从后台获取）
				"documentId" 		: $(this).children("td:nth-child("+(1)+")").text(),
				"documentName" 	: $(this).children("td:nth-child("+(2)+")").text(),
				"estimatescale" : $(this).children("td:nth-child("+(3)+")").text(),
				"actualscale" 	: $(this).find(".text").val(),
				"scalebias" 	: $(this).children("td:nth-child("+(5)+")").text()
		}
		jsonArray.push(jsonObj);
	});
    return jsonArray;
}

//获取快照表数据
function getData(snapshootnode){
	var path = "/"+getRootPath()+"/pmc/getWorkProductSnapshootInfoList.do";
	//动态调用数据
	$.ajax({
		cache:false,//是否缓存
		type:"POST",//提交方式
		url:path,
		data: {
		    "snapshoottype" : snapshoottype,
			"snapshootnode" : snapshootnode
		},
		async:false,//是否异步
		dataType:"json",
		error:function(request){//错误时
			alert("Connection?error");
		},
		success:function(data){
			dataList = data;
			tableContent = "";
			if(data[0].collectdate==null||data[0].collectdate==""){
				getCurrentDate();
			}else{
				$("#currentDate").html(data[0].collectdate);
			}
			for(var i=0;i<data.length;i++){
				tableContent += (""
					+"<tr>                                                               "
					+"	<td >"+data[i].id+"</td>                                          "
					+"	<td align='center' style='"+(data[i].actualscale <80?"color:red;":"")+"'>"+(data[i].documentname==null?"　":data[i].documentname)+"</td>        "
					+"	<td align='center' class='gj_guimo'  style='"+(data[i].actualscale <80?"color:red;":"")+"'>"+((null == data[i].estimateddocumentsize || 'undefined' == data[i].estimateddocumentsize)?"0":data[i].estimateddocumentsize)+"</td>"
					+"	<td align='center' ><input type='text' class='text' value='"+((null == data[i].actualscale || 'undefined' == data[i].actualscale)?"0":data[i].actualscale)+"' onkeypress ='return is_number(event,this)' style='"+(data[i].actualscale <80?"color:red;":"")+"'/></td>        "
					+"	<td align='center'><span class='PianCha' style='"+(data[i].actualscale <80?"color:red;":"")+"'>"+((null == data[i].scalebias || 'undefined' == data[i].scalebias)?"-":data[i].scalebias)+"</span></td>"
					+"</tr>"
					);
			}
			if(data.length<=0){
				tableContent+="<td colspan='4'>暂无快照</td></tr>";
			}
			$("#tb").html(tableContent);
		}
	});
}
//加载历史数据
function locadHistory(snapshootnode,obj){
	$(".sz").css("color","#C75F3E");
	$(obj).css("color","green");
	//加载数据
	if(snapshoottype=="doubleWeek"){
		window.open("/"+getRootPath()+'/keyware/kmflow/kpflow/pmc/workProductSituation/doubleWeekHistoryData.jsp?snapshootnode='+snapshootnode,'','width=750,height=550,top='+(screen.height-300)/2+',left='+(screen.width-750)/2+',status=yes,titlebar=no,toolbar=no,scrollbars=yes,resizable=yes');
	}else if(snapshoottype=="month"){
		window.open("/"+getRootPath()+'/keyware/kmflow/kpflow/pmc/workProductSituation/monthHistoryData.jsp?snapshootnode='+snapshootnode,'','width=750,height=550,top='+(screen.height-300)/2+',left='+(screen.width-750)/2+',status=yes,titlebar=no,toolbar=no,scrollbars=yes,resizable=yes');
	}
	$("#viewtable tr:eq(0)").find("th:eq(0)").hide();
	$("#tb tr").find("td:eq(0)").hide();
}

//计算规模偏差
function changePianCha(trNode) {
	//规模偏差
	var piancha = trNode.find(".PianCha");
	//估计规模
	var gj_guimo_val = trNode.find(".gj_guimo").text();
	//实际规模
	var sj_guimo_val = trNode.find(".text").val();
	if(""==sj_guimo_val || (sj_guimo_val*1)==0){
		piancha.html("-");
	}else{
		piancha.html(((sj_guimo_val/gj_guimo_val-1)*100).toFixed(2)+"%");
	}
}

//提交阶段报页面数据到阶段快照表中
function saveStageInfo(){
	var collectDate = $("#currentDate").text();
	var stageName = $("#stageName").text();
	var planStartDate = $("#planStartDate").text();
	var planFinishDate = $("#planFinishDate").text();
	var actualStartDate = $("#actualstartDate").text();
	var actualFinishDate = $("#actualfinishDate").text();
	var stageId = $("input[name='stageId']").val();
	var path="/"+getRootPath()+"/pmc/saveStageInfo.do";
	$.ajax({
		cache:false,
		type:"POST",
		url:path,
		async:false,
		dataType:"json",
		data : {
			"collectDate" : collectDate,
			"stageName"  : stageName,
			"planStartDate" : planStartDate,
			"planFinishDate" : planFinishDate,
			"actualStartDate" : actualStartDate,
			"actualFinishDate" : actualFinishDate,
			"stageId" : stageId,
			"parameter" : JSON.stringify(GetSubmitJsonData())
		},
		error:function(request){
			window.location.reload();
		},
		success:function(data){
			
		}
});
}
//获取阶段列表
function getStageList(){
	var path = "/"+getRootPath()+"/pmc/getStageList.do";
	$.ajax({
		cache:false,
		type:"post",
		url:path,
		async:false,
		dataType:"json",
		error:function(request){
		alert("错误链接！");
	},
		success:function(data){
		var dwContent = "<tr><td colspan='4'>阶段历史数据</td></tr>";
		var tdContent = "";
		var href = "";
		for(var i=0;i<data.length;i++){
			var obj = data[i];
			href = "<a style='cursor:hand;text-decoration: none' class='stage' onclick='openStageHistoryData(\""+obj.uid_+"\");'> ["+obj.name_+"]</a>";
			tdContent += (""
				+"<td>"+href+"</td>"
			);
			if((i+1)%4==0|| (i+1)==data.length){
				dwContent += ("<tr>"+tdContent+"</tr>");
				tdContent = "";
			}
		}
		$("#dw").html(dwContent);
	}
	});
}
//打开阶段历史数据
function openStageHistoryData(id){
	window.open("/"+getRootPath()+'/keyware/kmflow/kpflow/pmc/workProductSituation/stageHistoryData.jsp?stageId='+id,'','width=750,height=550,top='+(screen.height-300)/2+',left='+(screen.width-750)/2+',status=yes,titlebar=no,toolbar=no,scrollbars=yes,resizable=yes');
}
//获取所有里程碑
function getMileStoneList(){
	var path = "/"+getRootPath()+"/pmc/getAllMileStone.do";
	$.ajax({
		cache:false,
		type:"post",
		url:path,
		async:false,
		dataType:"json",
		error:function(request){
		alert("错误链接！");
	},
		success:function(data){
		var dwContent = "<tr><td colspan='4'>里程碑前期数据</td></tr>";
		var tdContent = "";
		var href = "";
		for(var i=0;i<data.length;i++){
			var obj = data[i];
			href = "<a style='cursor:hand;text-decoration: none' class='stage' onclick='openMileStoneHistoryData(\""+obj.uid_+"\");'> ["+obj.name_+"]</a>";
			tdContent += (""
				+"<td>"+href+"</td>"
			);
			if((i+1)%4==0|| (i+1)==data.length){
				dwContent += ("<tr>"+tdContent+"</tr>");
				tdContent = "";
			}
		}
		$("#dw").html(dwContent);
	}
	});
}
//打开里程碑历史数据
function openMileStoneHistoryData(id){
	window.open("/"+getRootPath()+'/keyware/kmflow/kpflow/pmc/workProductSituation/mileStoneHistoryData.jsp?stageId='+id,'','width=750,height=550,top='+(screen.height-300)/2+',left='+(screen.width-750)/2+',status=yes,titlebar=no,toolbar=no,scrollbars=yes,resizable=yes');
}
/*获取应用路径*/
function getRootPath(){
	var p = self.location+"";	
	var pp = p.split("/");	
	return (pp[3]);
}